<?php
require_once('browser.php');

function MusicBrowse() {
    $Repertoire = './Data/Musiques';
    echo BrwsGallerie($Repertoire);
}

// fonction qui revoye les informations d'une musique.   
function MusicInfo() {
    require_once('getid3/getid3.php');

    $musique = $_SESSION['Fichier'];
    $repertoire = $_SESSION['Repertoire'];
    // dossier
    $dir = $repertoire;

    // initialise la varianle play_list
    $play_list = '<?xml version="1.0" encoding="utf-8"?><config>';

    $play_list .= '<song>';

    //$file = "$dir/$file";
    $file = "$dir/$musique";
    $file .= '.mp3';

    $getID3 = new getID3;

    $ThisFileInfo = $getID3->analyze($file);

    getid3_lib::CopyTagsToComments($ThisFileInfo);

    // r�cup�re le titre
    if (isset($ThisFileInfo['tags']['id3v2']['title'])) {
        $play_list .= '<p class="info"><span class="categorie">Titre :</span> ' . $ThisFileInfo['tags']['id3v2']['title'][0] . '</p>';
    } else {
        $play_list .= '<p class="info"><span class="categorie">Titre :</span> iconnue</p>';
    }

    // r�cup�re l'artiste
    if (isset($ThisFileInfo['comments_html']['artist'])) {
        $play_list .= '<p class="info"><span class="categorie">Artiste :</span> ' . $ThisFileInfo['comments_html']['artist'][0] . '</p>';
    } else {
        $play_list .= '<p class="info"><span class="categorie">Artiste :</span> iconue</p>';
    }

    // r�cup�re l'album
    if (isset($ThisFileInfo['comments_html']['album'])) {
        $play_list .= '<p class="info"><span class="categorie">Album :</span> ' . $ThisFileInfo['comments_html']['album'][0] . '</p>';
    } else {
        $play_list .= '<p class="info"><span class="categorie">Album :</span> iconnue</p>';
    }

    // r�cup�re l'ann�e
    if (isset($ThisFileInfo['comments_html']['year'])) {
        $play_list .= '<p class="info"><span class="categorie">Annꥠ:</span> ' . $ThisFileInfo['comments_html']['year'][0] . '</p>';
    } else {
        $play_list .= '<p class="info"><span class="categorie">Annꥠ:</span> iconnue</p>';
    }

    // r�cup�re le genre
    if (isset($ThisFileInfo['tags']['id3v2']['genre'])) {
        $play_list .= '<p class="info"><span class="categorie">Genre :</span> ' . $ThisFileInfo['tags']['id3v2']['genre'][0] . '</p>';
    } else {
        $play_list .= '<p class="info"><span class="categorie">Genre :</span> iconnue</p>';
    }

    if (isset($ThisFileInfo['playtime_string'])) {
        $new_playtime = str_replace("[^A-Za-z0-9]", "", $ThisFileInfo['playtime_string']);

        // r�cup�re la dur�e
        $play_list .= '<p class="info"><span class="categorie">Duree :</span> ' . $new_playtime . '</p>';
    }
    // r�cup�re le ficheir
    $play_list .= '<p class="info"><span class="categorie">Chemin :</span> ' . $file . '</p>';

    $play_list .= '</config>';

    // affiche toutes les donn�es sur les musique
    echo "$play_list";
}

// fonction qui affiche le lecteur audio avec la musique choisie. 
function MusicPlay() {
    
    $repertoire = $_SESSION['Repertoire'];
    $Musique = $_SESSION['Fichier'];
    
    // Chemin de la musique
    $Chemin = $repertoire . '/' . $Musique . '.mp3';

    // Si le chemin de la musique existe...
    if (file_exists($Chemin)) {

        // Affiche le player
        echo '<audio id="audio" >
		<source src="' . $Chemin . '" type="audio/mpeg">
              </audio>';

        echo '<div id="PlayerAudio">';

        echo '<img src="IconePlayerAudio/button_play_red.png" onclick="document.getElementById(\'audio\').play()"></img>';
        echo '<img src="IconePlayerAudio/bouton-pause-rouge-icone-6219-48.png" onclick="document.getElementById(\'audio\').pause()"></img>';
        echo '<input type="range" step="any" id="seekbar"></input>';
        echo '<img class="Vol" src="IconePlayerAudio/media_volume_down.png" onclick="document.getElementById(\'audio\').volume -= 0.1"></img>';
        echo '<img class="Vol" src="IconePlayerAudio/media_volume_up.png" onclick="document.getElementById(\'audio\').volume += 0.1"></img>';

        echo '</div>';
        
        echo '<script type="text/javascript">
                var audio = document.getElementById("audio");
                var seekbar = document.getElementById("seekbar");

                seekbar.value = 0;

                function setupSeekbar() {
                    seekbar.min = audio.startTime;
                    seekbar.max = audio.startTime + audio.duration;
                }

                audio.ondurationchange = setupSeekbar;



                function seekAudio() {
                    audio.currentTime = seekbar.value;
                }


                function updateUI() {
                    var lastBuffered = audio.buffered.end(audio.buffered.length-1);
                    seekbar.min = audio.startTime;
                    seekbar.max = lastBuffered;
                    seekbar.value = audio.currentTime;
                }

                seekbar.onchange = seekAudio;
                audio.ontimeupdate = updateUI;	
                audio.addEventListener("durationchange", setupSeekbar);
                audio.addEventListener("timeupdate", updateUI);
                document.getElementById(\'audio\').play();
              </script>';
    }
    // Sinon affichage d'un message d'erreur
    else {
        echo '<h1 style="color : #D93A36;">Aucun fichier n\'est sélectionné</h1>';
    }
}
?>
